﻿namespace MicroCloud.Utils
{
    #region "<see cref="HttpRequestMessage"/> 扩展操作类"
    /// <summary>
    /// <see cref="HttpRequestMessage"/> 扩展操作类
    /// </summary>
    public static class HttpRequestMessageExtensions
    {
        #region "HttpRequestMessage 方法"
        #region "由旧的 <see cref="HttpRequestMessage"/> 和新数据创建新的 <see cref="HttpRequestMessage"/>"
        /// <summary>
        /// 由旧的 <see cref="HttpRequestMessage"/> 和新数据创建新的 <see cref="HttpRequestMessage"/>
        /// </summary>
        /// <param name="request"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static HttpRequestMessage CreateNew(this HttpRequestMessage request, string data)
        {
            HttpContent content = new StringContent(data);
            foreach (var header in request.Headers)
            {
                content.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }
            request.Content = content;
            return request;
        }
        #endregion

        #endregion

    }
    #endregion

    #region "<see cref="HttpResponseMessage"/> 扩展操作类"
    /// <summary>
    /// <see cref="HttpResponseMessage"/> 扩展操作类
    /// </summary>
    public static class HttpResponseMessageExtensions
    {
        #region "HttpResponseMessage 方法"
        #region "由旧的 <see cref="HttpResponseMessage"/> 和新数据创建新的 <see cref="HttpResponseMessage"/>"
        /// <summary>
        /// 由旧的 <see cref="HttpResponseMessage"/> 和新数据创建新的 <see cref="HttpResponseMessage"/>
        /// </summary>
        /// <param name="response"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static HttpResponseMessage CreateNew(this HttpResponseMessage response, string data)
        {
            HttpContent content = new StringContent(data);
            foreach (var header in response.Content.Headers)
            {
                content.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }
            response.Content = content;
            return response;
        }
        #endregion
        #region "尝试从 <see cref="HttpResponseMessage"/> 中提取 Cookie 值"
        /// <summary>
        /// 尝试从 <see cref="HttpResponseMessage"/> 中提取 Cookie 值
        /// </summary>
        /// <param name="response"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool TryGetCookie(this HttpResponseMessage response, string name, out string value)
        {
            return response.Headers.TryGetCookie(name, out value);
        }
        #endregion
        #region "尝试从 <see cref="HttpResponseMessage"/> 中提取 Refresh 的 URL"
        /// <summary>
        /// 尝试从 <see cref="HttpResponseMessage"/> 中提取 Refresh 的 URL
        /// </summary>
        /// <param name="response"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool TryGetRefreshUrl(this HttpResponseMessage response, out string url)
        {
            return response.Headers.TryGetRefreshUrl(out url);
        }
        #endregion

        #endregion

    }
    #endregion

}
